package com.bringway.p2p.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bringway.global.util.StringUtils;
import com.bringway.p2p.dao.TjrDao;
import com.bringway.p2p.service.TjrService;

/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月14日 上午9:34:42
 * @功能：抽奖结果页面
 */

@Service
@Transactional
public class TjrServiceImpl implements TjrService {

	private Logger logger = Logger.getLogger(TjrServiceImpl.class);

	@Autowired
	private TjrDao tjrDao;

	@Override
	public List querytjr(JSONObject params) {
		return tjrDao.querytjr(params);
	}

	@Override
	public int gettjrCount(JSONObject params) {
		return tjrDao.gettjrCount(params);
	}

	@Override
	public boolean updateTjr(JSONObject parms) {
		tjrDao.updateTjr(parms);
		return tjrDao.addTjr(parms) > 0;
	}

	@Override
	public List queryxTjr(JSONObject params) {
		return tjrDao.queryxTjr(params);
	}

	@Override
	public int queryxTjrcount(JSONObject params) {

		return tjrDao.tjrxcount(params);
	}

	@Override
	public boolean isCheckTjr(String username, String xtjr) {
		boolean isFirstTjr = tjrDao.isFirstTjr(username);
		if (isFirstTjr) {// 有一级推荐人,允许修改
			return true;
		} else {
			Map<String, String> tjrInfo = new HashMap<>();
			for (int i = 0; i < 2; i++) {
				tjrInfo = tjrDao.getTjrInfo(username);// 获取上级推荐人和用户名
				username = MapUtils.getString(tjrInfo, "USER_NAME", null);
				String tjr = MapUtils.getString(tjrInfo, "TJR", null);
				if (StringUtils.isNotBlank(tjr)) {
					return true;
				}
			}
			return false;
		}
	}

	@Override
	public List<Map<String, Object>> listUserTgInfo(JSONObject params) {
		return this.tjrDao.listUserTgInfo(params);
	}

	@Override
	public int getUserTgCount(JSONObject params) {
		return this.tjrDao.getUserTgCount(params);
	}

	@Override
	public boolean updateUserTggx() {
		List<String> userInfo = tjrDao.listUserInfo();
		for (String username : userInfo) {
			if (tjrDao.isNeedUpdate(username)) {
				logger.info("updateUserTggx 开始更新..." + username);
				try {
					tjrDao.updateTjrInfo(username);
				} catch (Exception e) {
					logger.info("updateUserTggx 更新报错...");
					e.printStackTrace();
					return false;
				}
			} else {
				logger.info("updateUserTggx 不需要更新..." + username);
			}
		}
		return true;
	}

	@Override
	@Transactional("p2p")
	public boolean updateTjjbByUsername(String username) {
		Map<String, String> paramMap = new HashMap<>();
		List<Map<String, String>> usernameAndTjr = tjrDao.listUsernameAndTjr(username);
		for (Map<String, String> map : usernameAndTjr) {
			if (MapUtils.isNotEmpty(map)) {
				username = MapUtils.getString(map, "USER_NAME", null);
				tjrDao.deleteTjjb(username);//删除原记录
				paramMap.put("USER_NAME", username);
				String tjr = MapUtils.getString(map, "TJR", null);
				paramMap.put("USER_NAME", username);
				paramMap.put("TJR_1", null);
				paramMap.put("TGCODE", null);
				paramMap.put("TJR_TGCODE", null);
				paramMap.put("TJR_USERNAME", null);
				paramMap.put("TJR_2", null);
				paramMap.put("TJR_TGCODE_2", null);
				paramMap.put("TJR_USERNAME_2", null);
				paramMap.put("TJR_3", null);
				paramMap.put("TJR", null);
				paramMap.put("TJJB", null);
				if (StringUtils.isNotBlank(tjr)) {
					paramMap.put("TJR_1", tjr);
					paramMap.put("TJR", tjr);
					paramMap.put("TJJB", "1");
				} else {
					Map<String, String> tjjbInfo2 = tjrDao.getTjjbMap(username);
					if (MapUtils.isNotEmpty(tjjbInfo2)) {
						paramMap.put("TJR_1", null);
						tjr = MapUtils.getString(tjjbInfo2, "TJR", null);
						if (StringUtils.isNotBlank(tjr)) {
							paramMap.put("TGCODE", MapUtils.getString(tjjbInfo2, "TGCODE", null));
							paramMap.put("TJR_TGCODE", MapUtils.getString(tjjbInfo2, "TJR_TGCODE", null));
							paramMap.put("TJR_USERNAME", MapUtils.getString(tjjbInfo2, "USER_NAME", null));
							paramMap.put("TJR_2", tjr);
							paramMap.put("TJR", tjr);
							paramMap.put("TJJB", "2");
						} else {
							Map<String, String> tjjbInfo3 = tjrDao.getTjjbMap(MapUtils.getString(tjjbInfo2, "USER_NAME", null));
							if (MapUtils.isNotEmpty(tjjbInfo3)) {
								paramMap.put("TJR_1", null);
								paramMap.put("TGCODE", null);
								paramMap.put("TJR_TGCODE", null);
								paramMap.put("TJR_USERNAME", null);
								paramMap.put("TJR_2", null);
								tjr = MapUtils.getString(tjjbInfo3, "TJR", null);
								if (StringUtils.isNotBlank(tjr)) {
									paramMap.put("TJR_TGCODE_2", MapUtils.getString(tjjbInfo3, "TJR_TGCODE", null));
									paramMap.put("TJR_USERNAME_2", MapUtils.getString(tjjbInfo3, "USER_NAME", null));
									paramMap.put("TJR_3", tjr);
									paramMap.put("TJR", tjr);
									paramMap.put("TJJB", "3");
								} 
							}
						}
					}
				}
				int saveTjjb = tjrDao.saveTjjb(paramMap);
				if (saveTjjb <= 0) {
					throw new RuntimeException();// 抛异常,回滚
				}
			}
		}
		return true;
	}

}
